<?php

namespace pam\Controller;

/**
 * Description of AgeingPolicyController
 *
 * @author Robert Bwana
 */
use Jaspersoft\Client\Client as JasperClient;
use pam\Service\PolicyServiceInterface as PSI;
use pam\Service\PolicyService;
use Zend\Mvc\Controller\AbstractActionController as AAC;
use Zend\View\Model\ViewModel;
use pam\Form\AgeingPolicyForm;
use pam\Model\AgeingPolicyStatement;
use pam\Model\Policy;
use pam\Form\PolicyForm;
use Zend\Form\FormInterface;

class AgeingPolicyController extends AAC {

    protected $ageingPolicyService;
    protected $policyService;
    protected $ageingForm;
    protected $policyForm;
    private $policy_num = 2;

    public function __construct(PSI $ageingPolicyService, PolicyService $policyService, FormInterface $form, FormInterface $policyForm) {
        $this->ageingPolicyService = $ageingPolicyService;
        $this->policyService = $policyService;
        $this->ageingForm = $form;
        $this->policyForm = $policyForm;
    }

    public function viewAction() {

        return new ViewModel(array(
            'ageingpolicies' => $this->ageingPolicyService->findAll(),
            'policy' => $this->policyService->findPolicy($this->policy_num),
            'goals' => $this->policyService->findGoals($this->policy_num)
        ));
    }

    public function printAction() {
        $this->exportReport();

        return $this->redirect()->toRoute('ageing');
    }

    public static function exportReport() {
        $client = new JasperClient(
                "http://reports.ssra.go.tz:8080/jasperserver", "jasperadmin", "jasperadmin"
        );
        $report = $client->reportService()->runReport('/reports/Policy/AgeingPolicyReport');

        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Description: File Transfer');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . strlen($report));
        header('Content-Disposition: attachment; filename=ageing-policy-report.pdf');
        header('Content-Type: application/pdf');

        echo $report;
        exit;
        //return new ViewModel();
    }

    public function addAction() {
        $request = $this->getRequest();

        if ($request->isPost()) {
            $this->ageingForm->setData($request->getPost());

            if ($this->ageingForm->isValid()) {
                try {

                    $this->ageingPolicyService->savePolicy($this->ageingForm->getData());

                    return $this->redirect()->toRoute('ageing');
                } catch (\Exception $e) {
                    die($e->getMessage());
                }
            }
        }

        return new ViewModel(array(
            'form' => $this->ageingForm
        ));
    }

    public function editAction() {
        $request = $this->getRequest();
        try {
            $policy = $this->ageingPolicyService->findPolicy($this->params('id'));
        } catch (\InvalidArgumentException $e) {
            return $this->redirect()->toRoute('ageing');
        }
        $this->ageingForm->bind($policy);

        if ($request->isPost()) {
            $this->ageingForm->setData($request->getPost());

            if ($this->ageingForm->isValid()) {
                try {
                    $this->ageingPolicyService->savePolicy($policy);

                    return $this->redirect()->toRoute('ageing');
                } catch (\Exception $e) {
                    die($e->getMessage());
                    // Some DB Error happened, log it and let the user know
                }
            }
        }

        return new ViewModel(array(
            'form' => $this->ageingForm
        ));
    }

    public function deleteAction() {
        try {
            $policy = $this->ageingPolicyService->findPolicy($this->params('id'));
        } catch (\InvalidArgumentException $e) {
            return $this->redirect()->toRoute('ageing');
        }

        $request = $this->getRequest();

        if ($request->isPost()) {
            $del = $request->getPost('delete_confirmation', 'No');

            if ($del === 'Yes') {
                $this->ageingPolicyService->deletePolicy($policy);
            }

            return $this->redirect()->toRoute('ageing');
        }

        return new ViewModel(array(
            'policy' => $policy
        ));
    }

    public function editPolicyAction() {
        $request = $this->getRequest();
        try {
            $policy = $this->policyService->findPolicy($this->policy_num);
        } catch (\InvalidArgumentException $e) {
            return $this->redirect()->toRoute('ageing');
        }
        $this->policyForm->bind($policy);

        if ($request->isPost()) {
            $this->policyForm->setData($request->getPost());

            if ($this->policyForm->isValid()) {
                try {
                    $this->policyService->savePolicy($policy);

                    return $this->redirect()->toRoute('ageing');
                } catch (\Exception $e) {
                    die($e->getMessage());
                    // Some DB Error happened, log it and let the user know
                }
            }
        }

        return new ViewModel(array(
            'form' => $this->policyForm
        ));
    }

}
